﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfHero;

namespace Tutorial
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public WHEngine Engine { get; set; }

        private Storyboard _showLoading;
        private Storyboard _startPlaying;
        private Storyboard _songFinished;
        private Storyboard _showLoadList;
        private bool _storyboardsInitialized = false;

        public Window1()
        {
            InitializeComponent();
            Engine = DataContext as WHEngine;
            Engine.SongFinished += new EventHandler(Engine_SongFinished);
            _showLoading = (Storyboard)Resources["ShowLoading"];
            _startPlaying = (Storyboard)Resources["StartPlaying"];
            _songFinished = (Storyboard)Resources["SongFinished"];
            _showLoadList = (Storyboard)Resources["ShowLoadList"];
        }

        void Engine_SongFinished(object sender, EventArgs e)
        {
            Engine.Stop();
            scroller.Stop();
            _songFinished.Begin(this);
        }

        private void StartGame(string path)
        {            
            Engine.Stop();
            Engine.Initialize(path, 1);
            App.DoEvents();
            scroller.Start();
            Engine.Start();
        }

        //private void StartGame(string path)
        //{
        //    Engine.Stop();

        //    if (!_storyboardsInitialized)
        //    {
        //        _showLoading.Completed += delegate
        //        {
        //            Engine.Initialize(path, 1);
        //            //TODO: startplaying does not play
        //            App.DoEvents();
        //            _startPlaying.Begin(this);
        //        };
        //        _startPlaying.Completed += delegate
        //        {
        //            scroller.Start();
        //            Engine.Start();
        //        };
        //        _storyboardsInitialized = true;
        //    }
        //    _showLoading.Begin(this);
        //}

        private void lbSongBrowser_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            SongBrowserDirSelected();
        }

        private void lbSongBrowser_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                SongBrowserDirSelected();
        }

        private void SongBrowserDirSelected()
        {
            SongBrowser sb = (SongBrowser)((ObjectDataProvider)Resources["SongBrowserDS"]).ObjectInstance;
            var selectedDir = (MyDirectory)lbSongBrowser.SelectedItem;
            sb.CurrentDirectory = selectedDir;

            if (selectedDir.HasSong)
                StartGame(selectedDir.FullPath);
        }

        private void newSongButton_Click(object sender, RoutedEventArgs e)
        {
            _showLoadList.Begin(this);
        }
    }
}
